﻿
using System.Data;
using Chloe.Infrastructure;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using Chloe.SQLite;
using System.Data.SQLite;
using PrismORM.ViewModels;
using Chloe;

namespace PrismORM
{

    public class PrismORMModule : IModule, IPrismORM
    {
        static Chloe.SQLite.SQLiteContext context;
        string connString = "Data Source=PrismORM.sqlite3;Version=3";
        public void RegisterTypes(IContainerRegistry containerRegistry)
        {

            context = new Chloe.SQLite.SQLiteContext(new SQLiteConnectionFactory(connString));
            containerRegistry.RegisterInstance(this);
            containerRegistry.Register<NameMsgModel>();

        }

        public void OnInitialized(IContainerProvider containerProvider)
        {
            //containerProvider.Resolve<IPrismORM>();
        }

        public T Insert<T>(T obj)
        {
           return context.Insert<T>(obj);
        }

        public int Delete<T1>(T1 obj)
        {
            return context.Delete(obj);
        }

        public int Update<T1>(T1 obj)
        {
          return  context.Update(obj);
        }
    
        public IQuery<T1> CreateQuery<T1>()
        {
            return context.Query<T1>();
        }

        public IDbContext GetDbContext()
        {
            return context;
        }

    }

    public class SQLiteConnectionFactory : IDbConnectionFactory
    {
        string _connString = null;
        public SQLiteConnectionFactory(string connString)
        {
            this._connString = connString;
        }
        public IDbConnection CreateConnection()
        {
            SQLiteConnection conn = new SQLiteConnection(this._connString);
            return conn;
        }
    }

}